package com.SNQ.project.English_speaking_learning_system.service.impl;

import com.SNQ.project.English_speaking_learning_system.common.constant.MessageConstant;
import com.SNQ.project.English_speaking_learning_system.common.constant.StatusConstant;
import com.SNQ.project.English_speaking_learning_system.common.exception.AccountLockedException;
import com.SNQ.project.English_speaking_learning_system.common.exception.AccountNotFoundException;
import com.SNQ.project.English_speaking_learning_system.common.exception.PasswordErrorException;
import com.SNQ.project.English_speaking_learning_system.common.properties.WeChatProperties;
import com.SNQ.project.English_speaking_learning_system.mapper.ArticleMapper;
import com.SNQ.project.English_speaking_learning_system.mapper.UserMapper;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.UserDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.UserLoginDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.entity.User;
import com.SNQ.project.English_speaking_learning_system.pojo.vo.UserVO;
import com.SNQ.project.English_speaking_learning_system.service.ArticleService;
import com.SNQ.project.English_speaking_learning_system.service.UserService;
import com.SNQ.project.English_speaking_learning_system.config.SpeechRecognizerDemo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleServiceImpl articleServiceImpl;

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    public User login(UserLoginDTO userLoginDTO) {

        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        User user = userMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (user.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        //3、返回实体对象
        return user;
    }

    public void enroll(UserDTO userDTO) {
        User user = new User();

        //对象属性拷贝
        BeanUtils.copyProperties(userDTO, user);

        //设置账号的状态，默认正常状态 1表示正常 0表示锁定
        user.setStatus(StatusConstant.ENABLE);

        //设置密码
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));

        //设置当前记录的创建时间
        user.setCreateTime(LocalDateTime.now());

        // 设置打卡次数初始值为 0
        user.setCheckinCount(0);

        user.setScores(0);

        userMapper.insert(user);
    }

    public List<UserVO> getArticleByCheckin() {
        return userMapper.getArticleByCheckin();
    }

    public List<UserVO> getArticleByScores() {
        return userMapper.getArticleByScores();
    }

    public List<UserVO> getDepartmentByCheckin(String department) {
        return userMapper.getDepartmentByCheckin(department);
    }

    public List<UserVO> getDepartmentByScores(String department) {
        return userMapper.getDepartmentByScores(department);
    }

    public List<UserVO> getMajorByCheckin(String department,String major) {
        return userMapper.getMajorByCheckin(department,major);
    }

    public List<UserVO> getMajorByScores(String department,String major) {
        return userMapper.getMajorByScores(department,major);
    }

    public List<UserVO> getClassNumberByCheckin(String department,String major,String classNumber) {
        return userMapper.getClassNumberByCheckin(department,major,classNumber);
    }

    public List<UserVO> getClassNumberByScores(String department,String major,String classNumber) {
        return userMapper.getClassNumberByScores(department,major,classNumber);
    }

    public int evaluateAudio(String audioUrl, Long articleId) throws Exception {
        // Step 1: 调用语音识别API将音频转成文字
        String recognizedText = SpeechRecognizerDemo.main(audioUrl);

        // Step 2: 根据articleId从数据库中查询文章内容
        String articleContent = articleMapper.getArticleContentById(articleId);

        // Step 3: 对比转文字内容和文章原文，根据相似程度打分
        int score = articleServiceImpl.compareTextSimilarity(recognizedText, articleContent);

        // Step 4: 返回计算出的分数
        return score;
    }


}
